package com.uroad.lib.widget.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.uroad.lib.R;
import com.uroad.lib.adapter.lv.CommonAdapter;
import com.uroad.lib.adapter.lv.ViewHolder;

import java.util.ArrayList;
import java.util.List;

public class DialogHelper {

    private static LoadingDialog dialog;

    /**
     * 弹出加载loading框
     *
     * @param c
     * @param msg
     */
    public static void showLoading(Context c, String msg) {

        try {
            if (dialog == null) {
                dialog = new LoadingDialog(c, R.style.baseCustomDialog);
                dialog.initLoading();
                dialog.setCanceledOnTouchOutside(false);
            }
            dialog.setMsg(msg);
            if (!dialog.isShowing())
                dialog.show();
        } catch (Exception e) {

        }

    }

    /**
     * 隐藏加载loading框
     */
    public static void endLoading() {
        try {
            if (dialog != null && dialog.isShowing()) {
                dialog.dismiss();
                dialog = null;
            }
        } catch (Exception e) {

        }

    }

    private static ComfirmDialog comfirmDialog;

    /**
     * 弹出对话框
     *
     * @param mContext
     * @param content
     */
    public static void showComfirmDialog(Context mContext, String content) {
        comfirmDialog = new ComfirmDialog(mContext);
        comfirmDialog.showDialog("", content);
    }

    public static void showComfirmDialog(Context mContext, String title, String content, String s, ComfirmDialog.DialogOnclick dialogOnclick, ComfirmDialog.DialogOnclick onclick) {
        comfirmDialog = new ComfirmDialog(mContext);
        comfirmDialog.showDialog(title, content);
    }

    public static void showComfrimDialog(Context c, String title, String msg,
                                         DialogInterface.OnClickListener okClickListener,
                                         DialogInterface.OnClickListener cancleClickListener) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(c).setTitle(title)
                .setMessage(msg).setPositiveButton("确定", okClickListener);
        if (cancleClickListener != null)
            dialog.setNegativeButton("", cancleClickListener);
        dialog.setCancelable(false);
        dialog.show();
    }
    public static void showComfirmDialog(Context mContext, String title, String content, String cancelText,
                                         String okText) {
        comfirmDialog = new ComfirmDialog(mContext);
        comfirmDialog.setButtonText(cancelText, okText);
        comfirmDialog.showDialog(title, content);
    }

    public static void showComfirmDialog(Context mContext, String title, String content, String cancelText,
                                         String okText, ComfirmDialog.DialogOnclick cancelListener,
                                         ComfirmDialog.DialogOnclick okListener) {
        comfirmDialog = new ComfirmDialog(mContext);
        comfirmDialog.setButtonText(cancelText, okText);
        comfirmDialog.setperClick(okListener, cancelListener);
        comfirmDialog.showDialog(title, content);
    }

    public static void showTipsDialog(Context mContext, String title, String content, ComfirmDialog.DialogOnclick okListener) {
        comfirmDialog = new ComfirmDialog(mContext);
        comfirmDialog.setperClick(okListener, null);
        comfirmDialog.showDialog(title, content);
    }


    public static void showLongTost(Context context, String mess) {
        try {
            LayoutInflater inflater = LayoutInflater
                    .from(context);
            View view = inflater.inflate(R.layout.view_toast, null);
            TextView textView = (TextView) view.findViewById(R.id.tvMsg);
            textView.setText(mess);
            Toast toast = new Toast(context);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(view);
            toast.show();
        } catch (Exception e) {
        }
    }

    public static void showShortTost(Context context, String mess) {
        try {
            LayoutInflater inflater = LayoutInflater
                    .from(context);
            View view = inflater.inflate(R.layout.view_toast, null);
            TextView textView = (TextView) view.findViewById(R.id.tvMsg);
            textView.setText(mess);
            Toast toast = new Toast(context);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setView(view);
            toast.show();
        } catch (Exception e) {
        }
    }

    private static TimeDialog timeDialog;

    public static void showTimeDialog(Context context, TimeDialog.DialogOnclick dialogOkClick) {
        timeDialog = new TimeDialog(context, dialogOkClick);
        timeDialog.show();
    }

    private static DateDialog dateDialog;

    public static void showDateDialog(Context context, DateDialog.DialogOnclick dialogOkClick) {
        dateDialog = new DateDialog(context, dialogOkClick);
        dateDialog.show();
    }

    private static PopupWindow popupWindow;
    private static LinearLayout ll_popup;
    private static RelativeLayout rl_pop_parent;
    private static ListView lv_pop;
    private static List<String> msgs = new ArrayList<String>();
    private static Activity activity;
    private static CommonAdapter<String> adapter;

    public static void initPopupWindow(Activity c, String title,
                                       final Itemclick listener) {

    }

    public static void showChooseWindow(Activity c, String title, List<String> list,
                                        final Itemclick listener,View view1) {
        activity = c;
        popupWindow = new PopupWindow(c);
        View popView = c.getLayoutInflater().inflate(R.layout.pop_common, null);
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        //popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(false);
        popupWindow.setContentView(popView);
        ll_popup = (LinearLayout) popView.findViewById(R.id.ll_popup);
        rl_pop_parent = (RelativeLayout) popView
                .findViewById(R.id.rl_pop_parent);
        TextView tv_title = (TextView) popView.findViewById(R.id.tv_title);
        if (!TextUtils.isEmpty(title)) {
            tv_title.setText(title);
        } else {
            tv_title.setVisibility(View.GONE);
        }

        lv_pop = (ListView) popView.findViewById(R.id.listview);
        adapter = new CommonAdapter<String>(activity, R.layout.view_item_pop_common, msgs) {
            @Override
            protected void convert(ViewHolder viewHolder, String item, int position) {
                viewHolder.setText(R.id.tvContent, item);
            }
        };
        lv_pop.setAdapter(adapter);
        lv_pop.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {
                listener.OnItemClickListener(arg2);
                popupWindow.dismiss();
            }
        });
        msgs.clear();
        msgs.addAll(list);
        adapter.notifyDataSetChanged();
        popupWindow.showAtLocation(view1, Gravity.BOTTOM, 0, 0);
        ll_popup.startAnimation(AnimationUtils.loadAnimation(activity,
                R.anim.base_push_bottom_in));
    }

    public interface Itemclick {
        void OnItemClickListener(int index);


    }
}
